package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Blog;
import com.kun.index.entity.Comment;
import com.kun.index.mapper.CommentMapper;
import com.kun.index.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public Integer insertComment(Comment comment) {
        return  commentMapper.insert(comment);
    }

    @Override
    public List<Comment> queryCommentsByBlogId(Integer blogId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blogId",blogId);
        return commentMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> queryComments(Map map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if (map.get("queryCommentUserId")!=null){
            Integer queryCommentUserId= (Integer) map.get("queryCommentUserId");
            wrapper.eq("userId",queryCommentUserId);
        }
        if (map.get("queryCommentBlogId")!=null){
            Integer queryCommentBlogId= (Integer) map.get("queryCommentBlogId");
            wrapper.eq("blogId",queryCommentBlogId);
        }

        if (map.get("pageIndex")!=null && map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Comment> page = new Page<>(pageIndex,pageSize);
            commentMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Comment> comments = page.getRecords();
            resultMap.put("totalCommentNum",total);
            resultMap.put("comments",comments);
        }else {
            List<Comment> comments = commentMapper.selectList(wrapper);
            resultMap.put("comments",comments);
        }
        return resultMap;
    }

    @Override
    public Comment queryCommentById(Integer pId) {
        return commentMapper.selectById(pId);
    }

    @Override
    public int batchDeleteComment(List<Integer> ids) {
        return commentMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteCommentById(Integer id) {
        return commentMapper.deleteById(id);
    }

    @Override
    public Integer updateComment(Comment comment) {
        return commentMapper.updateById(comment);
    }

    @Override
    public List<Comment> queryCommentsByRid(Integer rId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("rId",rId);
        return commentMapper.selectList(wrapper);
    }

    @Override
    public void recursionDeleteNotRootComments(Comment comment) {
        //1. 删除该节点
        this.deleteCommentById(comment.getId());
        //2. 遍历所有节点，满足pid和rid的进入递归
        List<Comment> comments = commentMapper.selectList(null);
        for (Comment comment1 : comments) {
            if (comment1.getRId()==comment.getRId() && comment1.getPId()==comment.getId()){     //二者同根且为父子关系
                this.recursionDeleteNotRootComments(comment1);
            }
        }
    }

}
